// 1.类的继承
class Person {
    name: string
    age: number
    constructor(name: string, age: number){
        this.name = name
        this.age = age
    }
}
class Student extends Person {
    sno: number
    constructor(name: string, age: number, sno: number) {
        super(name, age)    //super来调用父类的构造方法,对父类中的属性进行初始化
        this.sno = sno
    }

    studying() {
        console.log(this.name + "  studying");
    }
}
const lion = new Student('lion',18,1);
//Student { name: 'lion', age: 18, sno: 1}
console.log(lion);

//2.类的成员修饰符
//public    任何地方可见,公有的属性或方法,默认编写的属性是public
//private 修饰的是仅在同一类中可见,私有的属性和方法
//protected 修饰的是仅在类自身及子类中可见,受保护的属性或方法
class Person2{
    protected name: string  //受保护属性

    constructor(name: string){
        this.name = name
    }
}

class Student2 extends Person2{
    constructor(name: string){
        super(name)
    }
    running(){
        console.log(this.name+" running");
    }
}

const s2 = new Student2('s2')
// console.log(s2.name); 会报错,属性“name”受保护，只能在类“Person2”及其子类中访问。
s2.running()


class Person3{
    private name: string    //私有属性

    constructor(name: string){
        this.name = name
    }
}

const p = new Person3("why")
// console.log(p.name);    //会报错,属性“name”为私有属性，只能在类“Person3”中访问


//3. 只读属性readonly  该属性我们不希望外界可以任意修改,只希望确定值后直接使用,那么可以使用readonly
class Person4 {
    readonly name: string

    constructor(name: string){
        this.name = name
    }
    nickname(){
        // this.name += "1" //不能修改name 无法分配到 "name" ，因为它是只读属性。
        return "happy 的"+this.name
    }
}

const p2 = new Person4("why")  
console.log(p2.nickname());  //happy 的why 